package xyz.bali16.application.core.utils;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import xyz.bali16.application.entity.User;
import xyz.bali16.application.model.RoleGrantedAuthority;
import xyz.bali16.application.service.UserService;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class AuthUtils {
    private AuthUtils() {
    }


    /**
     * 获取当前登录用户名
     *
     * @return
     */
    public static String getCurrentUsername() {
        Authentication authentication = getCurrentAuth();
        if (authentication == null) {
            return null;
        }
        Object principal = authentication.getPrincipal();
        if (principal != null) {
            return String.valueOf(principal);
        } else {
            return null;
        }
    }

    /**
     * 获取当前授权用户
     *
     * @return
     */
    public static Collection<? extends GrantedAuthority> getCurrentGrantedAuthority() {
        Authentication authentication = getCurrentAuth();
        System.out.println("authentication");
        System.out.println(authentication);
        if (authentication == null) {
            return Collections.emptyList();
        }
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        if (authorities != null) {
            return authorities;
        } else {
            return Collections.emptyList();
        }
    }

    public static Set<String> getAuthoritySet() {
        Collection<? extends GrantedAuthority> userAuthorities = getCurrentGrantedAuthority();
        return AuthorityUtils.authorityListToSet(userAuthorities);
    }

    /**
     * 获取当前登录用户
     *
     * @return
     */
    public static Authentication getCurrentAuth() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 获取当前用户的角色代码
     */
    public static Set<String> getCurrentRoleIds() {
        Collection<? extends GrantedAuthority> authorities = getCurrentGrantedAuthority();

        if (authorities.isEmpty()) {
            return Collections.emptySet();
        }
        // 可以节省一定资源
        Set<String> roleCodes = Sets.newHashSetWithExpectedSize(authorities.size());
        for (GrantedAuthority grantedAuthority : authorities) {
            String authority = grantedAuthority.getAuthority();
            if (StrUtil.isEmpty(authority)) {
                continue;
            }
            if (authority.startsWith(RoleGrantedAuthority.ROLE_GRANTED_AUTHORITY_PREFIX)) {
                // 添加角色代码
                roleCodes.add(authority.replace(RoleGrantedAuthority.ROLE_GRANTED_AUTHORITY_PREFIX, ""));
            }
        }
        return roleCodes;
    }

    public static String getCurrentRoleId() {
        Collection<? extends GrantedAuthority> authorities = getCurrentGrantedAuthority();

        if (authorities.isEmpty()) {
            return "";
        }
        // 可以节省一定资源
        List<String> roleCodes = Lists.newArrayList();
        for (GrantedAuthority grantedAuthority : authorities) {
            String authority = grantedAuthority.getAuthority();
            if (StrUtil.isEmpty(authority)) {
                continue;
            }
            if (authority.startsWith(RoleGrantedAuthority.ROLE_GRANTED_AUTHORITY_PREFIX)) {
                // 添加角色代码
                roleCodes.add(authority.replace(RoleGrantedAuthority.ROLE_GRANTED_AUTHORITY_PREFIX, ""));
            }
        }
        return roleCodes.get(0);
    }
}
